<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Java," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="今天写了一个计算器，感觉不错，来记录一下，我把它改成JavaScript放在一个网页中可以运行。https://www.yanhaoblog.com/com.html这个计算器支持连续运算，小数点，括号。
比如你输入1.5 *2*((20+1)-1) 我就能返回给你答案60.0，大家刚接触编程的时候，一定做过一个简单计算器输入1选择+然后在输入1等于2。这太简单了。于是我写了一个支持输入运算式的。">
<meta property="og:type" content="article">
<meta property="og:title" content="Java计算器">
<meta property="og:url" content="https://www.yanhaoblog.com/2017/03/14/Java计算器/index.html">
<meta property="og:site_name" content="闫浩的博客">
<meta property="og:description" content="今天写了一个计算器，感觉不错，来记录一下，我把它改成JavaScript放在一个网页中可以运行。https://www.yanhaoblog.com/com.html这个计算器支持连续运算，小数点，括号。
比如你输入1.5 *2*((20+1)-1) 我就能返回给你答案60.0，大家刚接触编程的时候，一定做过一个简单计算器输入1选择+然后在输入1等于2。这太简单了。于是我写了一个支持输入运算式的。">
<meta property="og:updated_time" content="2017-03-19T13:23:20.969Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java计算器">
<meta name="twitter:description" content="今天写了一个计算器，感觉不错，来记录一下，我把它改成JavaScript放在一个网页中可以运行。https://www.yanhaoblog.com/com.html这个计算器支持连续运算，小数点，括号。
比如你输入1.5 *2*((20+1)-1) 我就能返回给你答案60.0，大家刚接触编程的时候，一定做过一个简单计算器输入1选择+然后在输入1等于2。这太简单了。于是我写了一个支持输入运算式的。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"right","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://www.yanhaoblog.com/2017/03/14/Java计算器/"/>





  <title> Java计算器 | 闫浩的博客 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">闫浩的博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle">Sometimes you got to run before you can walk</p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="https://www.yanhaoblog.com/2017/03/14/Java计算器/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="闫浩">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="/images/demo.png">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="闫浩的博客">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="闫浩的博客" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Java计算器
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-03-14T16:26:18+08:00">
                2017-03-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/03/14/Java计算器/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2017/03/14/Java计算器/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>今天写了一个计算器，感觉不错，来记录一下，我把它改成JavaScript放在一个网页中可以运行。<a href="https://www.yanhaoblog.com/com.html">https://www.yanhaoblog.com/com.html</a><br>这个计算器支持连续运算，小数点，括号。</p>
<p>比如你输入1.5 <code>*</code>2<code>*</code>((20+1)-1) 我就能返回给你答案60.0，大家刚接触编程的时候，一定做过一个简单计算器输入1选择+然后在输入1等于2。这太简单了。于是我写了一个支持输入运算式的。<br>先说一下我的思路,完全按照数学逻辑，首先考虑括号，先算1+1X2-3÷1，因为数字和运算符都在一起，所以我打算分开放，数字放在一个集合里，运算符放在一个集合里。至于怎么放就循环这个字符串所有字符，如果是数字或者小数点就拼起来，如果是加就把数字提交到数字集合，并把加放到运算符集合。<br>经过以上处理，我们得到两个集合 分别是{1，1，2，3，1}和{+, <code>*</code>,-,/}<br><a id="more"></a><br>好了准备好两个集合，我们就可以开始计算了，还是遵循数学运算规则，先算乘除，循环判断运算符集合中有没有乘或者除，有的话再判断这个是乘还是除，如果是乘，那么用这个乘下标去取数字集合中的数字，比如我上面的集合中乘的下标为i，那么我就用数字集合中下标为i的值乘以下表是i+1的值。然后把运算符集合中的i移除掉，并且把数字集合中i移除两次，最后把运算的结果放在数字集合中第i个位置。<br>这样新的数组就变成{1，2，3，1}和{+,-,/},然后在调用此方法，递归判断是否还有乘除再来一次后结果就变成{1,2,3}和{+,-}</p>
<p>这样乘除就计算完了，然后在计算加减就可以了同理，在此就不解释了。</p>
<p>好了最后再说括号，在此基础上，增加一个方法，去括号，如何合理的找到一对括号呢，我冥思苦想，终于屡清这个逻辑了，先找到最后一个（,然后在这个左括号的位置去继续往下找第一个右括号。<br>找到左右括号的位置，把括号中的内容截取下来，调用之前的无括号计算方法即可。<br>以下是我的代码，待优化。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.util.ArrayList;</div><div class="line"><span class="keyword">import</span>	 java.util.List;</div><div class="line"><span class="keyword">import</span> java.util.Scanner;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Compteion</span> </span>&#123;</div><div class="line">	<span class="comment">//去过括号的值</span></div><div class="line">	<span class="keyword">private</span> <span class="keyword">static</span> String remobrackets=<span class="keyword">new</span> String();</div><div class="line">	<span class="comment">//去括号</span></div><div class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">remoBrackets</span><span class="params">(String str)</span></span>&#123;</div><div class="line">		remobrackets=str;</div><div class="line">		System.out.println(<span class="string">"str="</span>+str);</div><div class="line">		<span class="keyword">int</span> lindex=-<span class="number">1</span>;</div><div class="line">		<span class="keyword">int</span> rindex=-<span class="number">1</span>;</div><div class="line">		<span class="comment">//先找到最后一个左括号</span></div><div class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; str.length(); i++) &#123;</div><div class="line">			<span class="keyword">if</span>(<span class="string">"("</span>.equals(str.charAt(i)+<span class="string">""</span>)||<span class="string">")"</span>.equals(str.charAt(i)+<span class="string">""</span>))&#123;</div><div class="line">				<span class="keyword">if</span>(<span class="string">"("</span>.equals(str.charAt(i)+<span class="string">""</span>))&#123;</div><div class="line">					lindex=i;</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		<span class="comment">//如果找到左括号，那么从左括号的位置找第一个右括号</span></div><div class="line">		<span class="keyword">if</span>(lindex!=-<span class="number">1</span>)&#123;</div><div class="line">			<span class="keyword">for</span> (<span class="keyword">int</span> i = lindex; i &lt; str.length(); i++) &#123;</div><div class="line">				<span class="keyword">if</span>(<span class="string">")"</span>.equals(str.charAt(i)+<span class="string">""</span>))&#123;</div><div class="line">					rindex=i;</div><div class="line">					<span class="keyword">break</span>;</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">			<span class="comment">//如果有一对括号，那么计算内容 结果替代原来的位置</span></div><div class="line">			<span class="keyword">if</span>(lindex!=-<span class="number">1</span>&amp;&amp;rindex!=-<span class="number">1</span>)&#123;</div><div class="line">				String subString=<span class="keyword">new</span> String(str);</div><div class="line">				String equation=subString.substring(lindex+<span class="number">1</span>,rindex);</div><div class="line">				System.out.println(<span class="string">"(括号中内容)"</span>+equation);</div><div class="line">				loadNumber(equation);</div><div class="line">				Double value=frontinspect(operator, number);</div><div class="line">				str=str.substring(<span class="number">0</span>,lindex)+value+<span class="string">""</span>+str.substring(rindex+<span class="number">1</span>,str.length());</div><div class="line">				lindex=-<span class="number">1</span>;</div><div class="line">				rindex=-<span class="number">1</span>;</div><div class="line">				operator=<span class="keyword">new</span> ArrayList&lt;Character&gt;();</div><div class="line">				number=<span class="keyword">new</span> ArrayList&lt;Double&gt;();</div><div class="line">				System.out.println(<span class="string">"(将括号内容计算后公式内容)"</span>+str);</div><div class="line">				remobrackets=str;</div><div class="line">				remoBrackets(str);</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="comment">//运算符储存</span></div><div class="line">	<span class="keyword">private</span> List&lt;Character&gt; operator=<span class="keyword">new</span> ArrayList&lt;Character&gt;();</div><div class="line">	<span class="comment">//数字储存</span></div><div class="line">	<span class="keyword">private</span> List&lt;Double&gt; number=<span class="keyword">new</span> ArrayList&lt;Double&gt;();</div><div class="line">	<span class="comment">//加载运算</span></div><div class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">loadNumber</span><span class="params">(String express)</span></span>&#123;</div><div class="line">		String expression=express;</div><div class="line">		String num=<span class="keyword">new</span> String();</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;expression.length();i++)&#123;</div><div class="line">			<span class="keyword">char</span> ch=expression.charAt(i);</div><div class="line">			<span class="keyword">if</span>(Character.isDigit(ch)||<span class="string">"."</span>.equals(ch+<span class="string">""</span>))&#123;</div><div class="line">				num+=ch;</div><div class="line">			&#125;</div><div class="line">			<span class="keyword">if</span>(i==expression.length()-<span class="number">1</span>)&#123;</div><div class="line">				<span class="keyword">if</span>(!num.isEmpty())&#123;</div><div class="line">					number.add(Double.parseDouble(num));</div><div class="line">					num=<span class="string">""</span>;</div><div class="line">				&#125;</div><div class="line">			&#125;</div><div class="line">			<span class="keyword">switch</span>(ch)&#123;</div><div class="line">				<span class="keyword">case</span> <span class="string">'+'</span>:</div><div class="line">					<span class="keyword">if</span>(!num.isEmpty())&#123;</div><div class="line">						number.add(Double.parseDouble(num));</div><div class="line">						num=<span class="string">""</span>;</div><div class="line">					&#125;</div><div class="line">					operator.add(<span class="string">'+'</span>);<span class="keyword">break</span>;</div><div class="line">				<span class="keyword">case</span> <span class="string">'-'</span>:</div><div class="line">					<span class="keyword">if</span>(!num.isEmpty())&#123;</div><div class="line">						number.add(Double.parseDouble(num));</div><div class="line">						num=<span class="string">""</span>;</div><div class="line">					&#125;</div><div class="line">					operator.add(<span class="string">'-'</span>);<span class="keyword">break</span>;</div><div class="line">				<span class="keyword">case</span> <span class="string">'*'</span>:</div><div class="line">					<span class="keyword">if</span>(!num.isEmpty())&#123;</div><div class="line">						number.add(Double.parseDouble(num));</div><div class="line">						num=<span class="string">""</span>;</div><div class="line">					&#125;</div><div class="line">					operator.add(<span class="string">'*'</span>);<span class="keyword">break</span>;</div><div class="line">				<span class="keyword">case</span> <span class="string">'/'</span>:</div><div class="line">					<span class="keyword">if</span>(!num.isEmpty())&#123;</div><div class="line">						number.add(Double.parseDouble(num));</div><div class="line">						num=<span class="string">""</span>;</div><div class="line">					&#125;</div><div class="line">					operator.add(<span class="string">'/'</span>);<span class="keyword">break</span>;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="comment">//计算乘除</span></div><div class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">multiplication</span><span class="params">(List&lt;Character&gt; operator,List&lt;Double&gt; number)</span></span>&#123;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;operator.size();i++)&#123;</div><div class="line">			<span class="keyword">if</span>(operator.get(i).equals(<span class="string">'*'</span>))&#123;<span class="comment">//如果有乘除先算乘除</span></div><div class="line">				System.out.println(<span class="string">"开始计算乘 "</span>+number.get(i)+<span class="string">"X"</span>+number.get(i+<span class="number">1</span>)+<span class="string">"="</span>+(number.get(i)*number.get(i+<span class="number">1</span>)));</div><div class="line">				<span class="keyword">double</span> val=number.get(i)*number.get(i+<span class="number">1</span>);</div><div class="line">				operator.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.add(i, val);</div><div class="line">				<span class="keyword">break</span>;</div><div class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(operator.get(i).equals(<span class="string">'/'</span>))&#123;</div><div class="line">				System.out.println(<span class="string">"开始计算除 "</span>+number.get(i)+<span class="string">"÷"</span>+number.get(i+<span class="number">1</span>)+<span class="string">"="</span>+(number.get(i)/number.get(i+<span class="number">1</span>)));</div><div class="line">				<span class="keyword">double</span> val=number.get(i)/number.get(i+<span class="number">1</span>);</div><div class="line">				operator.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.add(i, val);</div><div class="line">				<span class="keyword">break</span>;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="comment">//计算加减</span></div><div class="line">	<span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">plus</span><span class="params">(List&lt;Character&gt; operator,List&lt;Double&gt; number)</span></span>&#123;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;operator.size();i++)&#123;</div><div class="line">			<span class="keyword">if</span>(operator.get(i).equals(<span class="string">'+'</span>))&#123;<span class="comment">//如果有乘除先算乘除</span></div><div class="line">				System.out.println(<span class="string">"开始计算加 "</span>+number.get(i)+<span class="string">"+"</span>+number.get(i+<span class="number">1</span>)+<span class="string">"="</span>+(number.get(i)+number.get(i+<span class="number">1</span>)));</div><div class="line">				<span class="keyword">double</span> val=number.get(i)+number.get(i+<span class="number">1</span>);</div><div class="line">				operator.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.add(i, val);</div><div class="line">				<span class="keyword">break</span>;</div><div class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(operator.get(i).equals(<span class="string">'-'</span>))&#123;</div><div class="line">				System.out.println(<span class="string">"开始计算减 "</span>+number.get(i)+<span class="string">"-"</span>+number.get(i+<span class="number">1</span>)+<span class="string">"="</span>+(number.get(i)-number.get(i+<span class="number">1</span>)));</div><div class="line">				<span class="keyword">double</span> val=number.get(i)-number.get(i+<span class="number">1</span>);</div><div class="line">				operator.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.remove(i);</div><div class="line">				number.add(i, val);</div><div class="line">				<span class="keyword">break</span>;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="comment">//计算。。。。。。</span></div><div class="line">	<span class="function"><span class="keyword">private</span> Double <span class="title">frontinspect</span><span class="params">(List&lt;Character&gt; operator,List&lt;Double&gt; number)</span></span>&#123;</div><div class="line">		<span class="keyword">for</span> (Character ope : operator) &#123;</div><div class="line">			<span class="keyword">if</span>(ope.equals(<span class="string">'*'</span>)||ope.equals(<span class="string">'/'</span>))&#123;<span class="comment">//如果有乘除先算乘除</span></div><div class="line">				System.out.println(<span class="string">"计算乘除"</span>);</div><div class="line">				multiplication(operator, number);</div><div class="line">				frontinspect(operator, number);</div><div class="line">				<span class="keyword">break</span>;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		<span class="comment">//计算乘除后再计算加减</span></div><div class="line">		<span class="keyword">for</span> (Character ope : operator) &#123;</div><div class="line">			<span class="keyword">if</span>(ope.equals(<span class="string">'+'</span>)||ope.equals(<span class="string">'-'</span>))&#123;<span class="comment">//如果有乘除先算乘除</span></div><div class="line">				System.out.println(<span class="string">"计算加减"</span>);</div><div class="line">				plus(operator, number);</div><div class="line">				frontinspect(operator, number);</div><div class="line">				<span class="keyword">break</span>;</div><div class="line">			&#125;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">return</span> number.get(<span class="number">0</span>);</div><div class="line">	&#125;</div><div class="line">	<span class="comment">//源方法</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Calculator</span><span class="params">(String e)</span></span>&#123;</div><div class="line">		System.out.println(<span class="string">"需要计算的表达式"</span>+e);</div><div class="line">		loadNumber(e);</div><div class="line">		System.out.println(frontinspect(operator, number));</div><div class="line">	&#125;</div><div class="line">	<span class="comment">//显示List中元素</span></div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">shouList</span><span class="params">(List list)</span></span>&#123;</div><div class="line">		<span class="keyword">for</span> (Object object : list) &#123;</div><div class="line">			System.out.println(object+<span class="string">""</span>);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">		Scanner s = <span class="keyword">new</span> Scanner(System.in);</div><div class="line">		System.out.println(<span class="string">"请输入计算式"</span>);</div><div class="line">		Compteion com=<span class="keyword">new</span> Compteion();</div><div class="line">		<span class="comment">//去括号</span></div><div class="line">		com.remoBrackets(s.next());</div><div class="line">		System.out.println(remobrackets);</div><div class="line">		com.Calculator(remobrackets);</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Java/" rel="tag"># Java</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/03/10/简单工厂/" rel="next" title="简单工厂">
                <i class="fa fa-chevron-left"></i> 简单工厂
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/03/14/JS判断浏览器及版本号/" rel="prev" title="JS判断浏览器及版本号">
                JS判断浏览器及版本号 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2017/03/14/Java计算器/"
           data-title="Java计算器" data-url="https://www.yanhaoblog.com/2017/03/14/Java计算器/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/demo.png"
               alt="闫浩" />
          <p class="site-author-name" itemprop="name">闫浩</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">14</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">闫浩</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>


<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

<div class="powered-by">
Hosted by <a href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a>
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>



        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"yanhaods"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  












  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

  


</body>
</html>
